package MtG.server.components;

import java.util.Iterator;
/**
 * BroadCaster.java is the class broadcasts info 
 * to all the clients connected to the server. 
 * where all the cards in play will be situated.
 * @author Marek Sirp
 * @author Jaan Sepp
 */
public class BroadCaster extends Thread{

		/**
		 * Reference to the activeSession object of this server.
		 */
		private ActiveSession activeSession;
		/**
		 * Reference to the gameMoves object of this server.
		 */
		private GameMoves gameMove;

		/**
		 * Constructor.
		 * @param activeSession this servers activesession.
		 * @param gameMove this servers gamemove.
		 */
		public BroadCaster(ActiveSession activeSession, GameMoves gameMove) {
			this.activeSession = activeSession;
			this.gameMove = gameMove;
			start();
		}

		/* (non-Javadoc)
		 * @see java.lang.Thread#run()
		 */
		public void run() {
			while (true) {
				Object object = gameMove.getGameMove();
				synchronized (activeSession) {
					Iterator<Communication> active = activeSession.iterator();
					
					while (active.hasNext()) {
						Communication communication = active.next();
						
						if (!communication.isAlive()) {
							active.remove();
						} else { 
							communication.sendGameMove(object);
						}
					}
				}
			}
			
		}

}
